// $Id: type_traits.cpp 35 2009-01-11 19:55:40Z rodlima $

/*
   Copyright (c) 2008, Rodolfo Schulz de Lima 
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.  
   * Neither the name of RodSoft nor the names of its contributors may be used
     to endorse or promote products derived from this software without specific
     prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/

#include "pch.h"
#include "db.h"
#include "type_traits.h"

namespace orm
{

using namespace boost::posix_time;
using namespace boost::gregorian;

std::string type_traits<std::string>::to_sql_field(const transaction &xa, const type &val)/*{{{*/
{
    std::ostringstream ss;
    ss << '\'' << xa.escape(val) << '\'';
    return ss.str();
}/*}}}*/

std::string type_traits<time_duration>::to_string(const type &val)/*{{{*/
{
    if(val.is_not_a_date_time())
        return "";
    else
    {
        if(val.is_neg_infinity())
            return "-infinity";
        else if(val.is_pos_infinity())
            return "+infinity";
        else if(!val.is_special())
            return to_simple_string(val);
        else
            throw std::runtime_error("Invalid time_duration");
    }
}
std::string type_traits<time_duration>::to_sql_field(const transaction &xa, const type &val)
{
    if(val.is_not_a_date_time())
        return "NULL";
    else
        return orm::to_sql_field(xa,to_string(val));
}
time_duration type_traits<time_duration>::from_sql_field(const boost::optional<std::string> &str)
{
    if(str)
        return duration_from_string(*str);
    else
        return not_a_date_time;
}/*}}}*/

std::string type_traits<date>::to_string(const type &val)/*{{{*/
{
    if(val.is_not_a_date())
        return "";
    else
        return to_iso_extended_string(val);
}
std::string type_traits<date>::to_sql_field(const transaction &xa, const type &val)
{
    if(val.is_not_a_date())
        return "NULL";
    else
        return orm::to_sql_field(xa,to_string(val));
}
date type_traits<date>::from_sql_field(const boost::optional<std::string> &str)
{
    if(str)
        return from_string(*str);
    else
        return type(not_a_date_time);
}/*}}}*/

std::string type_traits<ptime>::to_string(const type &val)/*{{{*/
{
    if(val.is_not_a_date_time())
        return "";
    else
    {
        if(val.is_neg_infinity())
            return "-infinity";
        else if(val.is_pos_infinity())
            return "+infinity";
        else if(!val.is_special())
        {
            std::ostringstream ss;
            static std::locale loc(ss.getloc(), new time_facet("%Y-%m-%d %H:%M:%S"));
            ss.imbue(loc);
            ss << val;
            return ss.str();
        }
        else
            throw std::runtime_error("Invalid time");
    }
}
std::string type_traits<ptime>::to_sql_field(const transaction &xa, const type &val)
{
    if(val.is_not_a_date_time())
        return "NULL";
    else
        return orm::to_sql_field(xa,to_string(val));
}
ptime type_traits<ptime>::from_sql_field(const boost::optional<std::string> &str)
{
    if(str)
        return time_from_string(*str);
    else
        return not_a_date_time;
}/*}}}*/

std::string type_traits<bool>::to_string(bool val)/*{{{*/
{
    return val?"true":"false";
}
std::string type_traits<bool>::to_sql_field(const transaction &, bool val)
{
    return to_string(val);
}
bool type_traits<bool>::from_sql_field(const boost::optional<std::string> &str)
{
    if(str)
    {
        if(*str == "t")
            return true;
        else if(*str == "f")
            return false;
        else
            throw std::runtime_error("Bad boolean value: " + *str);
    }
    else
        return false;
}/*}}}*/

std::string get_sql_field_type(field_type_t type)/*{{{*/
{
    switch(type)
    {
    case INTEGER:
        return "integer";
    case BOOLEAN:
        return "boolean";
    case REAL:
        return "real";
    case TIME:
        return "time";
    case DATE:
        return "date";
    case TEXT:
        return "text";
    case INTERVAL:
        return "interval";
    default:
        assert(false);
        return "";
    }
}/*}}}*/

} // namespace orm

namespace boost 
{ 
    namespace posix_time
    {
        size_t hash_value(const time_duration &dur)/*{{{*/
        {
            size_t seed = 0;
            if(dur.is_special())
            {
                if(dur.is_neg_infinity())
                    seed = -1;
                else if(dur.is_pos_infinity())
                    seed = 1;
                else if(dur.is_not_a_date_time())
                    seed = 0;
            }
            else
                hash_combine(seed,dur.total_nanoseconds());
            return seed;
        }/*}}}*/
        size_t hash_value(const ptime &ptime)/*{{{*/
        {
            size_t seed = 0;
            hash_combine(seed,ptime.date());
            hash_combine(seed,ptime.time_of_day());
            return seed;
        }/*}}}*/
    }
    namespace gregorian
    {
        size_t hash_value(const date &date)/*{{{*/
        {
            size_t seed = 0;
            if(date.is_special())
            {
                if(date.is_neg_infinity())
                    seed = -2;
                else if(date.is_pos_infinity())
                    seed = 2;
                else if(date.is_not_a_date())
                    seed = 0;
            }
            else
            {
                date::ymd_type ymd = date.year_month_day();
                hash_combine(seed,ymd.year);
                hash_combine(seed,ymd.month);
                hash_combine(seed,ymd.day);
            }
            return seed;
        }/*}}}*/
    }
}

